package cn.edu.njpi.two;

import java.io.IOException;
import java.net.Socket;

public class Session {
    public static void main(String[] args) {
        String ip = "127.0.0.1";
        int port = 11111;
        try {
            Socket socket = new Socket(ip, port);
            socket.setSoTimeout(5539900);
            java.io.OutputStream out = socket.getOutputStream();
            byte[] date = "hello world".getBytes();
            out.write(date);
            out.flush();
            byte[] buffer = new byte[1024];
            int len = -1;
            java.io.FileOutputStream fout = new java.io.FileOutputStream(
                    "d:/response.txt");
            java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
            java.io.InputStream in = socket.getInputStream();
            while ((len = in.read(buffer, 0, buffer.length)) > 0) {
                bout.write(buffer, 0, len);
            }
            in.close();
            bout.flush();
            bout.close();
            byte[] rdata = bout.toByteArray();
            System.out.println(new String(rdata));
            fout.write(rdata);
            fout.flush();
            fout.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
